home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 19 / madtrb11.zip / XTONTIME.PAS < prev   
Pascal/Delphi Source File  |  1986-01-21  |  4KB  |  139 lines

  1. program x_to_n_time;
  2. { This program demonstrates the time differences between a number }
  3. { of the standard exponential functions and the x_to_n function.  }
  4. {  Modified and expanded by Claire A. Rinehart 1/6/86             }
  5.  
  6. Var
  7.  Iy,n        :  integer;
  8.  y,t1,t2  :  real;
  9.                        {   These results were obtained on an IBM PCjr         }
  10.                        {   x type      n type     result type     time (sec)  }
  11. {$I time.inc }
  12. {$I xton.inc }         {    real       integer       real            2.31     }
  13. {   power.inc }        {    real       integer       real            6.65     }
  14. {   Ipower.inc }       {   integer     integer       integer         1.65     }
  15. {   Ixton.inc }        {   integer     integer       integer         0.10     }
  16. {   Rpower.inc }       {    real       integer       real           15.77     }
  17. {   Mpower.inc }       {    real       integer       real           14.55     }
  18. {$I PwrR.inc }         {    real       real          real            6.76     }
  19. {$I PwrI.inc }         {    real       integer       real            2.86     }
  20.  
  21. Function IXtoN(x : integer;   n : integer) : integer;
  22. {  This include file contains the x_to_n function that computes  }
  23. {  the Nth power of an integer number, where N is an integer.    }
  24. {  Author :  Herb Holden                                         }
  25. {  Date   :  11/17/84                                            }
  26. {  Application : All systems.                                    }
  27. {                                                                }
  28. {  This function was taken from TUG newsletter vol I, issue 5    }
  29. {  pg. 32.                                                       }
  30. {  Modified to be a pure integer function by Claire A. Rinehart  }
  31.  
  32. Var
  33.   p : integer;
  34.   y : real;
  35. Begin               {computes x to the nth power}
  36.   if x = 0 then
  37.     begin
  38.       if n = 0 then
  39.         begin       {0 to the 0 is undefined}
  40.           writeln('** error in IxTOn:  x=0, n=0 **');
  41.            y := 1/0
  42.         end
  43.       else
  44.         IxTOn := 0   {0 to the n is 0}
  45.     end
  46.   else
  47.     begin
  48.       if n < 0 then      {x to -n is (1/x) to n and can not be assigned to an integer variable}
  49.         begin
  50.           writeln('ERROR!!  Attempted to assign a fraction to an integer.');
  51.           y := 1/0;
  52.         end;
  53.       if odd(n) then     {prepare for while loop}
  54.         p := x
  55.       else
  56.         p := 1;          {x to 0 will be 1}
  57.       n := n shr 1;
  58.       while n <> 0 do
  59.         begin
  60.           x := sqr(x);
  61.           if odd(n) then p := p * x;
  62.           n := n shr 1
  63.         end;
  64.       IxTOn := p
  65.     end
  66. end;  {IxTOn}
  67. function Ipwr(num,expon : integer): integer;
  68. {Use this function with care.  Numbers between 32 and 64 k are represented    }
  69. {as their negative counterparts because turbo handles integers as unsigned    }
  70. {values in the following manner:  0, 1,...,32766,32767,-32768,-32767,...,-2,-1}
  71. {This can be confusing because a negative integer raised to an odd power will }
  72. {also generate a negative result.                                             }
  73. Var
  74.   y : real;
  75. begin
  76.   if expon < 0 then
  77.    begin
  78.     writeln('Error!!  Exponent can not be negative');
  79.     y := 1/0;
  80.    end;
  81.   if expon = 0 then Ipwr :=1
  82.   else
  83.   Ipwr := num * Ipwr(num, expon - 1);
  84. end; {Ipwr}
  85. function pwr(num : real; expon : integer): real;
  86. begin
  87.   pwr := exp(expon * ln(num));
  88. end; {pwr}
  89.  
  90. Begin
  91.   t1 := time;
  92.   for n := 1 to 100 do
  93.     y := xton(1.1,n);    {5.5 sec}
  94.   t2 := time;
  95.   writeln('Time for xton:  ',t2-t1:6:2);
  96.  
  97.   t1 := time;
  98.   for n := 1 to 100 do
  99.     y := pwr(1.1,n);
  100.   t2 := time;
  101.   writeln('Time for pwr:  ',t2-t1:6:2);
  102.  
  103.   t1 := time;
  104.   for n := 1 to 100 do
  105.     Iy := Ipwr(1,n);
  106.   t2 := time;
  107.   writeln('Time for Ipwr:  ',t2-t1:6:2);
  108.  
  109.   t1 := time;
  110.   for n := 1 to 100 do
  111.     Iy := IxTOn(1,n);
  112.   t2 := time;
  113.   writeln('Time for IxTOn:  ',t2-t1:6:2);
  114.  
  115.   t1 := time;
  116.   for n := 1 to 100 do
  117.     y := Rpwr(1.1,n);
  118.   t2 := time;
  119.   writeln('Time for Rpwr:  ',t2-t1:6:2);
  120.  
  121.   t1 := time;
  122.   for n := 1 to 100 do
  123.     y := Mpwr(1.1,n);
  124.   t2 := time;
  125.   writeln('Time for Mpwr:  ',t2-t1:6:2);
  126.  
  127.   t1 := time;
  128.   for n := 1 to 100 do
  129.     y := Pwrr(1.1,n+0.1);
  130.   t2 := time;
  131.   writeln('Time for PwrR:  ',t2-t1:6:2);
  132.  
  133.   t1 := time;
  134.   for n := 1 to 100 do
  135.     y := PwrI(1.1,n);
  136.   t2 := time;
  137.   writeln('Time for PwrI:  ',t2-t1:6:2);
  138. end.
  139.